package com.lnj.community.utiles;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

public class CommunityUtil {
    /**
     * 生成随机字符串
     * @return 随机字符串
     */
    public static String generateUUID() {
        return java.util.UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * MD5加密 只能加密不能解密
     * @param key 要加密的字符串
     * @return 加密后的字符串
     */
    public static String md5(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        return DigestUtils.md5DigestAsHex(key.getBytes());
    }

    public static String getJSONString(int code, String msg, Map<String, Object> map) {
        JSONObject json = new JSONObject();
        try {
            json.put("code", code);
            json.put("msg", msg);
            for (String key : map.keySet()) {
                json.put(key, map.get(key));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    public static String getJSONString(int code, String msg) {
        return getJSONString(code, msg, new HashMap<>());
    }
    public static String getJSONString(int code) {
        return getJSONString(code, null, new HashMap<>());
    }


}
